IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[tr_CleanFiles_Upd]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[tr_CleanFiles_Upd]
GO

CREATE PROCEDURE [dbo].[tr_CleanFiles_Upd]
	@Archive INT,
	@Delete INT,
	@Purge INT
AS

-- Auto Archive

UPDATE 
	tr_File
SET
	ArchivedInd = 1
WHERE 
	tr_File.EntryDt > DATEADD(day, @Archive, tr_File.EntryDt)
	AND ArchivedInd = 0
	
-- Auto Delete

UPDATE
	tr_File
SET 
	DeletedInd = 1
WHERE
	tr_File.EntryDt > DATEADD(day, @Delete, tr_File.EntryDt)
	AND ArchivedInd = 1
	
-- Purge Deleted
	
DELETE FROM
	tr_File
WHERE 
	tr_File.EntryDt > DATEADD(day, @Purge, tr_File.EntryDt)	
	AND DeletedInd = 1;